<?php
/*****************************************************
       订单  Order        涉及数据表：mall_orders
    Programer:Liujy
    Create:2003-7-23
    Last updated:2004-8-10
******************************************************/
class Order
{
    var $orderId;            //订单ID
    var $shopId;            //商店ID
    var $userId;            //用户ID
    var $name;                //用户真实姓名
    var $address;            //送货地址
    var $zip;                //邮编
    var $tel;                //电话
    var $email;                //EMAIL
    var $mobile;            //EMAIL
    var $itemAmount = 0;    //订单物品总价
    var $freight;            //运费
    var $totalAmount;        //订单总价
    var $delivery;            //配送方式ID
    var $_delivery;            //配送方式
    var $payment;            //付款方式ID
    var $pptype;            //付款方式类型 2007-03-26 added
    var $_payment;            //付款方式
    var $paymentrate=0;        //支付费率
    var $payed = 0;            //付款状态0 未支付 1 已支付 2 处理中 3 部分付款 4 部分退款 5 全额退款
    var $_paystate;            //付款状态中文解释
    var $discount;            //折扣
    var $memo;                //备注
    var $ordertime;            //订单发送时间
    var $Ip;                //IP地址
    var $status;            //中文对应订单状态
    var $tmpstatus ;        //对应status的数字值
    var $items;                //订单商品明细对象
    var $limitStart = 0;
    var $limitLen = 10;
    var $rowsFound;
    var $dbObj;
    var $dbObj1;
    var $type;                //查询选择类型
    var $skey;                //查询选择值
    var $orderString;
    var $message;            //订单留言内容
    var $reply ;            //订单回复内容
    var $recsts;            //用户状态1 订单完成 2 订单取消 3已付款 4 订单完成 && 已付款 5 用户新留言
    var $canceltxt;            //取消订单原因
    var $userrecsts = 0;    //标识用户反馈0 没有新反馈，1 有新反馈，2 有新的回馈
    var $feedbacktime = 0;    //用户反馈時間
    var $ordstate=0;            //订单状态  0:未确认 1:已确认 2:部分发货 3:已发货 4.已归档 5.部分退货 6.全部退货 9.已取消
    var $ifinvoice=0;            //是否开发票
    var $invoiceform;            //发票抬头
    var $payid;                    //支付流水号
    var $historypayid;            //支付历史流水号
    var $paytime;                //支付日期
    var $paycur;                //支付币别
    var $paymoney;                //支付金额
    var $payaccount;                //支付帐号
    var $protect=0;                //保驾配送
    var $begtime="";            //查询起始时间
    var $endtime="";            //查询起始时间
    var $weight="";                //重量
    var $sendtime=0;            //配送时间
    var $province;            //送货省
    var $area = 0;            //送货地区ID
    var $deliveryArea = 0;            //送货地区
    var $listtype=0;        //列表类型   1:发货列表 2.退货列表 3.收款列表 4.退款列表
    var $orderpoint=0;        //订单总积分
    var $getpoint=0;        //用户取得积分
    var $consumepoint=0;        //消费积分
    var $chgpoint = 0;          //换购积分
    var $payallmark = 0;        //是否进行过全额退款操作
    var $couponsprice = 0;        //订单中使用的优惠券
    var $chgpointmoney = 0;          //换购积分
    var $chgpointrate = 0;          //换购积分时的兑换比例

    var $phone;  //混查tel或mobile


    //构造函数
    function Order(){
        $license = new mallLicense;    //License验证
        $this->dbObj = new YQ_SQL;    //初始化数据库对象
        $this->dbObj1  = new YQ_SQL;
        $this->orderString = " ORDER BY {$GLOBALS["_tbpre"]}mall_orders.ordertime DESC";
    }

    //根据ID加载订单
    function getbyId($id, $userid="")
    {
        if ($userid != "") $tmp_sql = " AND userid = '".$userid."' ";
        else $tmp_sql = "";
        global $PROG_TAGS;
        if (strlen($id)>13)//shopex订单
        $sqlString = "SELECT {$GLOBALS["_tbpre"]}mall_orders.*, {$GLOBALS["_tbpre"]}mall_offer_p.ptype as pptype
                      FROM {$GLOBALS["_tbpre"]}mall_orders
                      LEFT JOIN {$GLOBALS["_tbpre"]}mall_offer_p ON {$GLOBALS["_tbpre"]}mall_offer_p.id={$GLOBALS["_tbpre"]}mall_orders.ptype
                      WHERE {$GLOBALS["_tbpre"]}mall_orders.offerid=".$this->shopId." AND {$GLOBALS["_tbpre"]}mall_orders.orderid='".$id."'".$tmp_sql."
                      ORDER BY {$GLOBALS["_tbpre"]}mall_orders.ordertime DESC";
        else//ecshop转换过来的订单
        $sqlString="SELECT {$GLOBALS["_tbpre"]}mall_orders.* FROM {$GLOBALS["_tbpre"]}mall_orders
                      WHERE {$GLOBALS["_tbpre"]}mall_orders.offerid=".$this->shopId." AND {$GLOBALS["_tbpre"]}mall_orders.orderid='".$id."'".$tmp_sql."
                      ORDER BY {$GLOBALS["_tbpre"]}mall_orders.ordertime DESC";
        $this->dbObj->Query($sqlString);
        if(!$this->dbObj->next_record())
        {
            return False;
        }else{
            $this->orderId = $this->dbObj->f("orderid");
            $this->shopId = $this->dbObj->f("offerid");
            $this->userId = $this->dbObj->f("userid");
            $this->name = $this->dbObj->f("name");
            $this->address = $this->dbObj->f("addr");
            $this->zip = $this->dbObj->f("zip");
            $this->tel = $this->dbObj->f("tel");
            $this->email = $this->dbObj->f("email");
            $this->mobile = $this->dbObj->f("mobile");
            $this->itemAmount = $this->dbObj->f("item_amount");
            $this->freight = $this->dbObj->f("freight");
            $this->totalAmount = $this->dbObj->f("total_amount");
            $this->delivery = $this->dbObj->f("ttype");
            $this->payment = $this->dbObj->f("ptype");
            $this->_delivery = $this->dbObj->f("delivery");        //updated:2004-03-31 mender:liujy 新增该输出
            $this->_payment = $this->dbObj->f("payment");        //updated:2004-03-31 mender:liujy 新增该输出
            $this->payed = $this->dbObj->f("ifsk");
            $this->discount = $this->dbObj->f("discount");
            $this->memo = $this->dbObj->f("memo");
            $this->ordertime = $this->dbObj->f("ordertime");
            $this->Ip = $this->dbObj->f("ip");
            $this->message = $this->dbObj->f("message");
            $this->reply = $this->dbObj->f("reply");
            $this->recsts = $this->dbObj->f("recsts");
            $this->canceltxt = $this->dbObj->f("cancel");
            $this->userrecsts = $this->dbObj->f("userrecsts");
            $this->feedbacktime = $this->dbObj->f("feedbacktime");
            $this->ordstate = $this->dbObj->f("ordstate");
            $this->ifinvoice = $this->dbObj->f("ifinvoice");
            $this->invoiceform = $this->dbObj->f("invoiceform");
            $this->payid = $this->dbObj->f("payid");
            $this->paytime = $this->dbObj->f("paytime");
            $this->paycur = $this->dbObj->f("paycur");
            $this->area = $this->dbObj->f("area");
            $this->deliveryArea = $this->dbObj->f("deliveryarea");
            $this->paymoney = $this->dbObj->f("paymoney");
            $this->protect = $this->dbObj->f("protect");
            $this->payaccount = $this->dbObj->f("payaccount");
            $this->weight = $this->dbObj->f("weight");
            $this->sendtime = $this->dbObj->f("sendtime");
            $this->province = $this->dbObj->f("province");
            $this->paymentrate = $this->dbObj->f("paymentrate");
            $this->orderpoint = $this->dbObj->f("orderpoint");
            $this->getpoint = $this->dbObj->f("getpoint");
            $this->consumepoint = $this->dbObj->f("consumepoint");
            $this->chgpoint = $this->dbObj->f("chgpoint");
            $this->payallmark = $this->dbObj->f("payallmark");
            $this->historypayid = $this->dbObj->f("historypayid");

            $this->couponsprice = $this->dbObj->f("couponsprice");
            $this->chgpointmoney = $this->dbObj->f("chgpointmoney");
            $this->chgpointrate = $this->dbObj->f("chgpointrate");
            $this->pptype = $this->dbObj->f("pptype");

            switch ($this->ordstate)
            {
                case 0:
                    $this->status = $PROG_TAGS["ptag_370"];
                    $this->tmpstatus = 0;
                    break;
                case 1:
                    $this->status = $PROG_TAGS["ptag_232"];
                    $this->tmpstatus = 1;
                    break;
                case 2:
                    $this->status = $PROG_TAGS["ptag_1527"];
                    $this->tmpstatus = 2;
                    break;
                case 3:
                    $this->status = $PROG_TAGS["ptag_234"];
                    $this->tmpstatus = 3;
                    break;
                case 4:
                    $this->status = $PROG_TAGS["ptag_235"];
                    $this->tmpstatus = 4;
                    break;
                case 5:
                    $this->status = $PROG_TAGS["ptag_reshipedany"];
                    $this->tmpstatus = 5;
                    break;
                case 6:
                    $this->status = $PROG_TAGS["ptag_reshiped"];
                    $this->tmpstatus = 6;
                    break;
                case 9:
                    $this->status = $PROG_TAGS["ptag_369"];
                    $this->tmpstatus = 9;
                    break;
                default :
                    $this->status = $PROG_TAGS["ptag_370"];
                    $this->tmpstatus = 0;
                    break;
            }

            if ($this->payed == 1)
                $this->_paystate = $PROG_TAGS["ptag_1510"];
            elseif($this->payed == 2)
                $this->_paystate = $PROG_TAGS["ptag_2031"];
            elseif($this->payed == 3)
                $this->_paystate = $PROG_TAGS["ptag_2033"];
            elseif($this->payed == 4)
                $this->_paystate = $PROG_TAGS["ptag_2323"];
            elseif($this->payed == 5)
                $this->_paystate = $PROG_TAGS["ptag_2324"];
            else
                $this->_paystate = $PROG_TAGS["ptag_1511"];

            $this->items = newclass("orderItems");        //
            $this->items->orderId = $this->dbObj->f("orderid");
            $this->items->shopId = $this->dbObj->f("offerid");
            $this->items->getList();
            //$i = 0;
            //while($orderitems->next()){
            //    $this->items[$i] = $orderitems;
            //    $i++;
            //}

            return True;
        }
    }

    //根据payid查LIST
    function getListbyPayid($payid)
    {
        $sqlString = "select * from {$GLOBALS["_tbpre"]}mall_orders where offerid=".$this->shopId." and historypayid like '%".$payid."%' order by paytime desc";
        $this->dbObj->Query($sqlString);
        $this->rowsFound = $this->dbObj->nf();
    }

    //取得本日已支付
    function getPayedSumToday()
    {
        $sqlString = "SELECT sum(total_amount) as allsum FROM {$GLOBALS["_tbpre"]}mall_orders WHERE offerid=".intval($this->shopId)." AND orderid like '".date("Y").date("m").date("d")."%' AND (ifsk=1 OR ifsk=2) AND orderid<>'{$this->orderId}'";
        $this->dbObj->Query($sqlString);
        if($this->dbObj->next_record())
        {
            return $this->dbObj->f("allsum");
        }
        else return 0;

    }

    //新订单
    function toInsert(){
        $sqlString = "SELECT max(orderid) FROM {$GLOBALS["_tbpre"]}mall_orders WHERE offerid=".intval($this->shopId)." AND orderid like '".date("Y").date("m").date("d")."%'";
        $this->dbObj->Query($sqlString);
        $this->dbObj->next_record();
        $orderid = substr($this->dbObj->f(0),-4)+0;
        if ($orderid==9999){
            $orderid = 0;
        }

        $orderid = date("Y").date("m").date("d").date("H").intString(($orderid+1),4);
        $sqlString = "INSERT INTO {$GLOBALS["_tbpre"]}mall_orders (orderid, offerid, userid, name, addr, zip, tel, email,
                    item_amount, freight, total_amount, ttype, ptype, ifsk, memo, ordertime, discount,
                    ip, ifinvoice, invoiceform, ordstate, paycur, paymoney,area, protect, weight, mobile, deliveryarea,
                    sendtime, province,consumepoint,chgpoint,payallmark,couponsprice,chgpointmoney,chgpointrate) VALUES ('";

        $sqlString.= $orderid."',";
        $sqlString.= $this->shopId.",";
        $sqlString.= $this->userId.",'";
        $sqlString.= $this->name."','";
        $sqlString.= $this->address."','";
        $sqlString.= $this->zip."','";
        $sqlString.= $this->tel."','";
        $sqlString.= $this->email."',";
        $sqlString.= $this->itemAmount.",";
        $sqlString.= $this->freight.",";
        $sqlString.= $this->totalAmount.",'";
        $sqlString.= $this->delivery."','";
        $sqlString.= $this->payment."',";
        $sqlString.= $this->payed.",'";
        $sqlString.= $this->memo."',";
        $sqlString.= time().",'";
        $sqlString.= $this->discount."','";
        $sqlString.= $this->Ip."','";
        $sqlString.= $this->ifinvoice."','";
        $sqlString.= $this->invoiceform."','";
        $sqlString.= $this->ordstate."','";
        $sqlString.= $this->paycur."','";
        $sqlString.= $this->paymoney."','";
        $sqlString.= intval($this->area)."','";
        $sqlString.= $this->protect."','";
        $sqlString.= $this->weight."','";
        $sqlString.= $this->mobile."','";
        $sqlString.= $this->deliveryArea."','";
        $sqlString.= $this->sendtime."','";
        $sqlString.= $this->province."','";
        $sqlString.= $this->consumepoint."','";
        $sqlString.= $this->chgpoint."','";
        $sqlString.= $this->payallmark."','";
        $sqlString.= $this->couponsprice."','";
        $sqlString.= $this->chgpointmoney."','";
        $sqlString.= $this->chgpointrate."')";




//echo $sqlString;
        if ($this->dbObj->Query($sqlString)) {
            return $orderid;
        }else{
            return false;
        }
    }

    //加载订单记录列表，条件:shopId&userId或shopId
/*
area 配送地区
ttype配送方式
ptype支付方式
invoiceform发票抬头
paymoney订单金额
userid
用户名：         姓名：         电话：     订单附言：
地址：         email：

name收货人姓名
addr收货人地址
 tel 收货人固定电话 mobile 收货人移动电话   收货人电话

*/
    function getList($userid=0,$limit=0)
    {
        $sqlString = "SELECT {$GLOBALS["_tbpre"]}mall_orders.* FROM {$GLOBALS["_tbpre"]}mall_orders";

        if (trim($this->b_user)!='' || trim($this->b_name)!='' || trim($this->b_tel)!='' || trim($this->b_addr)!='' || trim($this->b_email)!='')
        {
            $sqlString .= " LEFT JOIN {$GLOBALS["_tbpre"]}mall_member ON {$GLOBALS["_tbpre"]}mall_member.userid={$GLOBALS["_tbpre"]}mall_orders.userid";
        }

        if (trim($this->o_bn)!='' || trim($this->o_goods)!=''){
            $sqlString .= " LEFT JOIN {$GLOBALS["_tbpre"]}mall_items ON {$GLOBALS["_tbpre"]}mall_items.orderid={$GLOBALS["_tbpre"]}mall_orders.orderid";
        }

        if (!empty($this->type) && !empty($this->skey)){
            if($this->type == "user ="){    //如果是查订货人，则关联到mall_member表
                $sqlString .= " LEFT JOIN {$GLOBALS["_tbpre"]}mall_member ON {$GLOBALS["_tbpre"]}mall_member.userid={$GLOBALS["_tbpre"]}mall_orders.userid WHERE {$GLOBALS["_tbpre"]}mall_member.".$this->type." '".$this->skey."' AND" ;
            }else if ($this->type == "historypayid"){
                $sqlString .= " where {$GLOBALS["_tbpre"]}mall_orders.".$this->type." like '%".$this->skey."%' AND" ;
            }else{
                $sqlString .= " where {$GLOBALS["_tbpre"]}mall_orders.".$this->type." '".$this->skey."' AND" ;
            }
        }else{
            $sqlString .= " WHERE";
        }
        if ($userid==0){            //得到查询语句
            $sqlString .= " {$GLOBALS["_tbpre"]}mall_orders.offerid=".$this->shopId;
        }else{
            $sqlString .= " {$GLOBALS["_tbpre"]}mall_orders.offerid=".$this->shopId." AND {$GLOBALS["_tbpre"]}mall_orders.userid=".$userid;
        }
        //--------------------------
        if ($this->o_area > 0){
            $sqlString .= " AND {$GLOBALS["_tbpre"]}mall_orders.area = ".$this->o_area;
        }
        if ($this->o_delivery > 0){
            $sqlString .= " AND {$GLOBALS["_tbpre"]}mall_orders.ttype = ".$this->o_delivery;
        }
        if ($this->o_payment > 0){
            $sqlString .= " AND {$GLOBALS["_tbpre"]}mall_orders.ptype = ".$this->o_payment;
        }
        if (trim($this->o_invoiceform) != ''){
            $sqlString .= " AND {$GLOBALS["_tbpre"]}mall_orders.invoiceform = '{$this->o_invoiceform}'";
        }
//
        if (trim($this->r_name) != ''){
            $sqlString .= " AND {$GLOBALS["_tbpre"]}mall_orders.name like '%{$this->r_name}%'";
        }
        if (trim($this->r_address) != ''){
            $sqlString .= " AND {$GLOBALS["_tbpre"]}mall_orders.addr like '{%$this->r_address}%'";
        }
        if (trim($this->r_phone) != ''){
            $sqlString .= " AND ({$GLOBALS["_tbpre"]}mall_orders.tel LIKE '%{$this->r_phone}%' OR {$GLOBALS["_tbpre"]}mall_orders.mobile LIKE '%{$this->r_phone}%')";
        }
        //--------------------------

        if (trim($this->b_user)!='')
        {
            $sqlString .= " AND {$GLOBALS["_tbpre"]}mall_member.user like '%{$this->b_user}%'";
        }

        if (trim($this->b_name)!='')
        {
            $sqlString .= " AND {$GLOBALS["_tbpre"]}mall_member.name = '%{$this->b_name}%'";
        }

        if (trim($this->b_tel)!='')
        {
            $sqlString .= " AND {$GLOBALS["_tbpre"]}mall_member.tel = '%{$this->b_tel}%'";
        }

        if (trim($this->b_addr)!='')
        {
            $sqlString .= " AND {$GLOBALS["_tbpre"]}mall_member.addr = '%{$this->b_addr}%'";
        }

        if (trim($this->b_email)!='')
        {
            $sqlString .= " AND {$GLOBALS["_tbpre"]}mall_member.email = '%{$this->b_email}%'";
        }
        //--------------------------
        if (intval($this->o_from_amount)>0 || intval($this->o_to_amount)>0)
        {
            if ($this->o_from_amount!=0)
            {
                $sqlString .= " AND {$GLOBALS["_tbpre"]}mall_orders.total_amount>={$this->o_from_amount}";
            }
            if ($this->o_to_amount!=0)
            {
                $sqlString .= " AND {$GLOBALS["_tbpre"]}mall_orders.total_amount<={$this->o_to_amount}";
            }
        }

        if (trim($this->o_bn)!='')
        {
            $sqlString .= " AND {$GLOBALS["_tbpre"]}mall_items.bn like '%{$this->bn}%'";
        }

        if (trim($this->o_goods)!='')
        {
            $sqlString .= " AND {$GLOBALS["_tbpre"]}mall_items.goods like '%{$this->o_goods}%'";
        }

        if (trim($this->o_message)!='')
        {
            $sqlString .= " AND {$GLOBALS["_tbpre"]}mall_orders.message like '%{$this->o_message}%'";
        }


        //--------------------------

        if ($this->userrecsts > 0){
            $sqlString .= " AND {$GLOBALS["_tbpre"]}mall_orders.userrecsts = ".$this->userrecsts;
        }

        if (isset($this->payed) && $this->payed != ""){
            $sqlString .= " AND {$GLOBALS["_tbpre"]}mall_orders.ifsk = ".$this->payed;
        }
        if (isset($this->ordstate) && $this->ordstate != ""){
            $sqlString .= " AND {$GLOBALS["_tbpre"]}mall_orders.ordstate = ".$this->ordstate;
        }
        if (isset($this->begtime) && $this->begtime != ""){
            $sqlString .= " AND {$GLOBALS["_tbpre"]}mall_orders.ordertime >= '".$this->begtime."'";
        }
        if (isset($this->endtime) && $this->endtime != ""){
            $sqlString .= " AND {$GLOBALS["_tbpre"]}mall_orders.ordertime <= '".$this->endtime."'";
        }
        switch($this->listtype)
        {
            case 1:  //发货
                $sqlString .= " AND (mall_orders.ordstate = 0 OR {$GLOBALS["_tbpre"]}mall_orders.ordstate = 1 OR {$GLOBALS["_tbpre"]}mall_orders.ordstate = 2)";
                break;
            case 2:  //退货
                $sqlString .= " AND (mall_orders.ordstate = 3 OR {$GLOBALS["_tbpre"]}mall_orders.ordstate = 2)";
                break;
            case 3:  //收款  支付状态为未支付,处理中,部分付款时,订单状态为非取消和非归档
                $sqlString .= " AND (mall_orders.ifsk = 0 OR {$GLOBALS["_tbpre"]}mall_orders.ifsk = 2 OR {$GLOBALS["_tbpre"]}mall_orders.ifsk = 3) AND {$GLOBALS["_tbpre"]}mall_orders.ordstate <> 4 AND {$GLOBALS["_tbpre"]}mall_orders.ordstate <> 9";
                break;
            case 4:  //退款  支付状态为已支付,部分退款,部分付款时,订单状态为非取消和非归档
                $sqlString .= " AND (mall_orders.ifsk = 1 OR {$GLOBALS["_tbpre"]}mall_orders.ifsk = 3 OR {$GLOBALS["_tbpre"]}mall_orders.ifsk = 4) AND {$GLOBALS["_tbpre"]}mall_orders.ordstate <> 4 AND {$GLOBALS["_tbpre"]}mall_orders.ordstate <> 9";
                break;
        }
        $sqlString .= $this->orderString ;
        if ($limit==1) {
            $sqlString.= " LIMIT ".$this->limitStart.",".$this->limitLen;
        }
        $this->dbObj->Query($sqlString);
        $this->rowsFound = $this->dbObj->nf();
    }

    /*********
    updated:2004-03-31 mender:liujy 未处理订单记录数显示
    读取未处理订单记录数，条件:shopId&userId或shopId
    *********/
    function getneworder($userid=0){

        if ($userid==0){            //得到查询语句
            $sqlString = "SELECT * FROM {$GLOBALS["_tbpre"]}mall_orders WHERE offerid=".$this->shopId." and ordstate = 0";
        }else{
            $sqlString = "SELECT * FROM {$GLOBALS["_tbpre"]}mall_orders WHERE offerid=".$this->shopId." AND userid=".$userid." and ordstate = 0";
        }
        $this->dbObj->Query($sqlString);
        return $this->dbObj->nf();

    }

    //更新订单记录
    function toUpdate()
    {
        $sqlString = "UPDATE {$GLOBALS["_tbpre"]}mall_orders SET ";
        $sqlString.= "userid= ".do_slash($this->userId);
        $sqlString.= ", name= '".do_slash($this->name);
        $sqlString.= "', addr= '".do_slash($this->address);
        $sqlString.= "', zip= '".do_slash($this->zip);
        $sqlString.= "', mobile= '".do_slash($this->mobile);
        $sqlString.= "', province= '".do_slash($this->province);
        $sqlString.= "', tel= '".do_slash($this->tel);
        $sqlString.= "', email= '".do_slash($this->email);
        $sqlString.= "', item_amount= ".do_slash($this->itemAmount);
        $sqlString.= ", freight= ".do_slash($this->freight);
        $sqlString.= ", total_amount= ".do_slash($this->totalAmount);
        $sqlString.= ", ttype= '".do_slash($this->delivery);
        $sqlString.= "', delivery= '".do_slash($this->_delivery);
        $sqlString.= "', ptype= '".do_slash($this->payment);
        $sqlString.= "', payment= '".do_slash($this->_payment);
        $sqlString.= "', ifsk= ".do_slash($this->payed);
        $sqlString.= ", ordstate= ".do_slash($this->ordstate);
        $sqlString.= ", memo= '".do_slash($this->memo);
        $sqlString.= "', discount= ".do_slash($this->discount);
        $sqlString.= ", ip= '".do_slash($this->Ip);
        $sqlString.= "', payid= '".do_slash($this->payid);
        $sqlString.= "', area= '".intval($this->area);
        $sqlString.= "', paycur= '".do_slash($this->paycur);
        $sqlString.= "', paymoney= '".do_slash($this->paymoney);
        $sqlString.= "', paymentrate= '".do_slash($this->paymentrate);
        $sqlString.= "', ifinvoice= '".intval($this->ifinvoice);
        $sqlString.= "', protect= '".do_slash($this->protect);
        $sqlString.= "', weight= '".do_slash($this->weight);
        $sqlString.= "', orderpoint= ".intval($this->orderpoint);
        $sqlString.= ", getpoint= ".intval($this->getpoint);
        $sqlString.= ", consumepoint= ".intval($this->consumepoint);
        $sqlString.= ", chgpoint= ".intval($this->chgpoint);
        $sqlString.= ", chgpointmoney= ".$this->chgpointmoney;
        $sqlString.= ", chgpointrate= ".intval($this->chgpointrate);
        $sqlString.= ", payallmark= ".intval($this->payallmark);



        $sqlString.= ", couponsprice= ".intval($this->couponsprice);

        $sqlString.= " WHERE orderid='".$this->orderId."' AND offerid=".$this->shopId;
//        echo $sqlString;
//        exit;
        $this->dbObj->Query($sqlString);
    }

    //删除订单，注意：确认后的订单不得删除！！！同时应删除订单相关明细记录。
    function toRemove(){
        $sqlString = "SELECT * FROM {$GLOBALS["_tbpre"]}mall_orders WHERE orderid='".$this->orderId."' AND offerid=".$this->shopId;
        $this->dbObj->Query($sqlString);
        if (!$this->dbObj->next_record()){
            return false;
        }else{
            $sqlString_1 = "DELETE FROM {$GLOBALS["_tbpre"]}mall_orders WHERE orderid='".$this->orderId."' AND offerid=".$this->shopId;
            $this->dbObj->Query($sqlString_1);
            $this->dbObj->Query("select id from {$GLOBALS["_tbpre"]}mall_items where orderid = '".$this->orderId."' AND offerid=".$this->shopId);
            while($this->dbObj->next_record())
            {
                $this->dbObj1->Query("DELETE FROM {$GLOBALS["_tbpre"]}mall_order_discreteness WHERE itemid='".$this->dbObj->f("id")."' AND offerid=".$this->shopId);
            }

            $sqlString_2 = "DELETE FROM {$GLOBALS["_tbpre"]}mall_items WHERE orderid='".$this->orderId."' AND offerid=".$this->shopId;
            $this->dbObj->Query($sqlString_2);

            $sqlString_2 = "DELETE FROM {$GLOBALS["_tbpre"]}mall_goodsnotify WHERE email='".$this->orderId."' AND offerid=".$this->shopId;
            $this->dbObj->Query($sqlString_2);
            return true;
        }
    }

    //订单编辑中使用，删除某些项，输入参数为保留的参数列表
    function toRemoveItems($keep_gidarr){
        $keepgid = implode(",",$keep_gidarr);
        $sqlString = "SELECT * FROM {$GLOBALS["_tbpre"]}mall_orders WHERE orderid='".$this->orderId."' AND offerid=".$this->shopId;
        $this->dbObj->Query($sqlString);
        if (!$this->dbObj->next_record()){
            return false;
        }else{
            $this->dbObj->Query("select id from {$GLOBALS["_tbpre"]}mall_items where orderid = '".$this->orderId."' and gid not in({$keepgid}) AND offerid=".$this->shopId);
            while($this->dbObj->next_record())
            {
                $this->dbObj1->Query("DELETE FROM {$GLOBALS["_tbpre"]}mall_order_discreteness WHERE itemid='".$this->dbObj->f("id")."' AND offerid=".$this->shopId);
            }
            $sqlString_2 = "DELETE FROM {$GLOBALS["_tbpre"]}mall_items WHERE orderid='".$this->orderId."' and gid not in({$keepgid}) AND offerid=".$this->shopId;
            $this->dbObj->Query($sqlString_2);
            return true;
        }

    }

    //取下一个订单
    function next()
    {
        global $PROG_TAGS;
        if(!$this->dbObj->next_record()){
            return False;
        }else{
            $this->orderId = $this->dbObj->f("orderid");
            $this->shopId = $this->dbObj->f("offerid");
            $this->userId = $this->dbObj->f("userid");
            $this->name = $this->dbObj->f("name");
            $this->address = $this->dbObj->f("addr");
            $this->zip = $this->dbObj->f("zip");
            $this->tel = $this->dbObj->f("tel");
            $this->email = $this->dbObj->f("email");
            $this->mobile = $this->dbObj->f("mobile");
            $this->itemAmount = $this->dbObj->f("item_amount");
            $this->freight = $this->dbObj->f("freight");
            $this->totalAmount = $this->dbObj->f("total_amount");
            $this->delivery = $this->dbObj->f("ttype");
            $this->payment = $this->dbObj->f("ptype");
            $this->_delivery = $this->dbObj->f("delivery");
            $this->_payment = $this->dbObj->f("payment");
            $this->payed = $this->dbObj->f("ifsk");
            $this->ordstate = $this->dbObj->f("ordstate");
            $this->discount = $this->dbObj->f("discount");
            $this->memo = $this->dbObj->f("memo");
            $this->ordertime = $this->dbObj->f("ordertime");
            $this->Ip = $this->dbObj->f("ip");
            $this->message = $this->dbObj->f("message");
            $this->reply = $this->dbObj->f("reply");
            $this->recsts = $this->dbObj->f("recsts");
            $this->canceltxt = $this->dbObj->f("cancel");
            $this->userrecsts = $this->dbObj->f("userrecsts");
            $this->feedbacktime = $this->dbObj->f("feedbacktime");
            $this->ordstate = $this->dbObj->f("ordstate");
            $this->ifinvoice = $this->dbObj->f("ifinvoice");
            $this->invoiceform = $this->dbObj->f("invoiceform");
            $this->payid = $this->dbObj->f("payid");
            $this->paytime = $this->dbObj->f("paytime");
            $this->payaccount = $this->dbObj->f("payaccount");
            $this->paycur = $this->dbObj->f("paycur");
            $this->paymoney = $this->dbObj->f("paymoney");
            $this->area = $this->dbObj->f("area");
            $this->deliveryArea = $this->dbObj->f("deliveryarea");
            $this->protect = $this->dbObj->f("protect");
            $this->weight = $this->dbObj->f("weight");
            $this->sendtime = $this->dbObj->f("sendtime");
            $this->province = $this->dbObj->f("province");
            $this->paymentrate = $this->dbObj->f("paymentrate");
            $this->orderpoint = $this->dbObj->f("orderpoint");
            $this->getpoint = $this->dbObj->f("getpoint");
            $this->consumepoint = $this->dbObj->f("consumepoint");
            $this->chgpoint = $this->dbObj->f("chgpoint");
            $this->chgpointmoney = $this->dbObj->f("chgpointmoney");
            $this->chgpointrate = $this->dbObj->f("chgpointrate");

            $this->payallmark = $this->dbObj->f("payallmark");
            $this->couponsprice = $this->dbObj->f("couponsprice");

            $this->historypayid = $this->dbObj->f("historypayid");

            switch ($this->ordstate)
            {
                case 0:
                    $this->status = $PROG_TAGS["ptag_370"];
                    $this->tmpstatus = 0;
                    break;
                case 1:
                    $this->status = $PROG_TAGS["ptag_232"];
                    $this->tmpstatus = 1;
                    break;
                case 2:
                    $this->status = $PROG_TAGS["ptag_1527"];
                    $this->tmpstatus = 2;
                    break;
                case 3:
                    $this->status = $PROG_TAGS["ptag_234"];
                    $this->tmpstatus = 3;
                    break;
                case 4:
                    $this->status = $PROG_TAGS["ptag_235"];
                    $this->tmpstatus = 4;
                    break;
                case 5:
                    $this->status = $PROG_TAGS["ptag_reshipedany"];
                    $this->tmpstatus = 5;
                    break;
                case 6:
                    $this->status = $PROG_TAGS["ptag_reshiped"];
                    $this->tmpstatus = 6;
                    break;
                case 9:
                    $this->status = $PROG_TAGS["ptag_369"];
                    $this->tmpstatus = 9;
                    break;
                default :
                    $this->status = $PROG_TAGS["ptag_370"];
                    $this->tmpstatus = 0;
                    break;
            }

            if ($this->payed == 1)
                $this->_paystate = $PROG_TAGS["ptag_1510"];
            elseif($this->payed == 2)
                $this->_paystate = $PROG_TAGS["ptag_2031"];
            elseif($this->payed == 3)
                $this->_paystate = $PROG_TAGS["ptag_2033"];
            elseif($this->payed == 4)
                $this->_paystate = $PROG_TAGS["ptag_2323"];
            elseif($this->payed == 5)
                $this->_paystate = $PROG_TAGS["ptag_2324"];
            else
                $this->_paystate = $PROG_TAGS["ptag_1511"];

            $this->items = newclass("orderItems");
            $this->items->orderId = $this->dbObj->f("orderid");
            $this->items->shopId = $this->dbObj->f("offerid");
            return True;
        }
    }

    //取订单总数
    function nf(){
        return $this->rowsFound;
    }

    //设置已付款标记
    function payed()
    {
        $sqlString = "select * from {$GLOBALS["_tbpre"]}mall_orders  WHERE orderid='".$this->orderId."' and ifsk <> 1 AND offerid=".$this->shopId;
        $this->dbObj->Query($sqlString);
        if($this->dbObj->next_record())
        {
            $INC_USERID = $this->dbObj->f("userid");
            $this->userId = $INC_USERID;
            $this->orderpoint = $this->dbObj->f("orderpoint");    //订单积分
            $this->getpoint = $this->dbObj->f("getpoint");    //订单获得积分
            $this->payed = $this->dbObj->f("ifsk");
            $sqlString="select point from {$GLOBALS["_tbpre"]}mall_mall_member_level as l,{$GLOBALS["_tbpre"]}mall_member as m where l.levelid=m.level and m.userid='".$INC_USERID."'";
            $this->dbObj->Query($sqlString);
            $point=$this->db->f("point");

            $sqlString = "UPDATE {$GLOBALS["_tbpre"]}mall_orders SET ifsk= 1, paytime=".time()."
                    WHERE orderid='".$this->orderId."' AND offerid=".$this->shopId;
            $this->dbObj->Query($sqlString);

            //开始设置会员积分
            if ($this->orderpoint > 0 && $this->getpoint==0 && $INC_USERID>0 && ($this->payed == 0 || $this->payed == 2 || $this->payed == 3)&&intval($point)>0)
            {
                $ShopUser = newclass("User");
                $ShopUser->userId = $INC_USERID;        //订单客户
                $ShopUser->getbyId();
                if ($ShopUser->username != "anonymous")
                {

                    $ShopUser->point += $this->orderpoint ;
                    $ShopUser->point_history += $this->orderpoint ;
                    $ShopUser->toUpdatelevel();        //更新会员积分级别

                    $sqlString = "UPDATE {$GLOBALS["_tbpre"]}mall_orders SET getpoint= ".intval($this->orderpoint)
                        ." WHERE orderid='".$this->orderId."' AND offerid=".$this->shopId;
                    $this->dbObj->Query($sqlString);


                    $pointHistory = newclass("pointHistory");
                    $pointHistory->userId = $INC_USERID;
                    $pointHistory->shopId = $this->shopId;
                    $pointHistory->point = $this->orderpoint;
                    $pointHistory->reason = 'buy_product';
                    $pointHistory->relatedId = $this->orderId;
                    $pointHistory->addHistory();
//                    echo $pointHistory->reason.'-'.$pointHistory->point;
//                    exit;
                }
            }
            $this->generateCoupons();//产生优惠券
        }
    }

    function generateCoupons()
    {
        $orderItems = newclass('orderItems');
        $orderItems->orderId = $this->orderId;
        $orderItems->shopId = $this->shopId;
        $orderItems->getlist();
        $gidArr = array();
        while ($orderItems->next())
        {
            $gidArr[] = $orderItems->gid;
        }
        $Coupon = newclass("Coupon");
        $Coupon->shopId = $this->shopId;
        $Coupon->orderGenerateCoupons($gidArr, $this->userId, $this->orderId);
    }

    //设置正在处理中标记
    function payprocess(){
        $sqlString = "UPDATE {$GLOBALS["_tbpre"]}mall_orders SET ifsk= 2 WHERE orderid='".$this->orderId."' AND offerid=".$this->shopId;
        $this->dbObj->Query($sqlString);
        $sqlString = "UPDATE {$GLOBALS["_tbpre"]}mall_items set paystate = 2";
        $sqlString.= " WHERE offerid=".$this->shopId;
        $sqlString.= " and orderid=".$this->orderId;
        $this->dbObj->Query($sqlString);
    }

    //设置在线已付款标记
    function onlinePayed($btime, $etime)
    {
        $sqlString = "select * from {$GLOBALS["_tbpre"]}mall_orders WHERE payid='".$this->payid."' and  (ifsk= 0 OR ifsk=2) AND paytime >= ".$btime." AND paytime < ".$etime;
        $this->dbObj->Query($sqlString);
        if($this->dbObj->next_record())
        {
            $INC_USERID = $this->dbObj->f("userid");
            $this->orderId = $this->dbObj->f("orderid");
            $this->paycur = $this->dbObj->f("paycur");
            $this->payment = $this->dbObj->f("ptype");
            $this->totalAmount = $this->dbObj->f("total_amount");
            $this->orderpoint = $this->dbObj->f("orderpoint");    //订单积分
            $this->getpoint = $this->dbObj->f("getpoint");    //订单获得积分
            $this->payed = $this->dbObj->f("ifsk");

            $sqlString = "UPDATE {$GLOBALS["_tbpre"]}mall_orders SET ifsk= 1, paytime=".time()."
                    WHERE payid='".$this->payid."' AND paytime >= ".$btime." AND paytime < ".$etime;
            $this->dbObj->Query($sqlString);
            //开始设置会员积分
			$ShopUser = newclass("User");
            $ShopUser->userId = $INC_USERID;        //订单客户
            $ShopUser->getbyId();
            if ($this->orderpoint > 0 && $this->getpoint == 0 && $INC_USERID > 0)
            {
                if ($ShopUser->username != "anonymous")
                {
                    $MemberLevel = newclass("MemberLevel");
                    $MemberLevel->shopId = $this->shopId;
                    $ShopUser->point += $this->orderpoint ;
                    $MemberLevel->getlevelbyPoint($ShopUser->point);    //当前客户级别
                    $ShopUser->level = $MemberLevel->levelId ;
                    $ShopUser->toUpdatelevel($this->shopId);        //更新会员积分、级别

                    $sqlString = "UPDATE {$GLOBALS["_tbpre"]}mall_orders SET getpoint= ".intval($this->orderpoint)
                        ." WHERE payid='".$this->payid."' AND paytime >= ".$btime." AND paytime < ".$etime;
                    $this->dbObj->Query($sqlString);
                }
            }

            /***********新增付款单据************/
            $shopPayment = newclass("shopPayment");
            $shopPayment->shopId = $this->shopId;
            $shopPayment->getbyId($this->payment);

            $shopCur = newclass("ShopCurrency");
            $shopCur->shopId = $this->shopId;
            $rate = $shopCur->getCurRate($this->paycur);

            $orderBill = newclass("orderBill");
            $orderBill->offer_id = $this->shopId;
            $orderBill->maker_id = 0;                //制单人ID
            $orderBill->maker_name = "ONLINEPAY";    //制单人姓名
            $orderBill->order_no = $this->orderId;    //订单号
            $orderBill->pay_id = $this->payid;    //支付流水号
            $orderBill->pay_type = $this->payment;    //付款类型
            $orderBill->pay_name = $shopPayment->name;    //付款类型名称
            $orderBill->pay_way = 0;        //货款收/退款方式
            $orderBill->acount = $shopPayment->merId;    //付款帐号
            $orderBill->bank = $shopPayment->type;    //付款银行
            $orderBill->person = $ShopUser->username;        //付款人
            $orderBill->price_def = $this->totalAmount;        //金额(本币)
            $orderBill->price = ($this->totalAmount * $rate);        //金额(支付币)
            $orderBill->currency = $this->paycur;        //付款货币
            $orderBill->ex_rate = 1;        //汇率
            $orderBill->status = 1;            //状态
            $orderBill->memo = $this->payid;    //备注
            $orderBill->bill_date = time();        //发生时间
            $orderBill->toInsert();
            /***********新增付款单据************/
        }
    }

    //订单确认
    function confirmed(){
        $sqlString = "UPDATE {$GLOBALS["_tbpre"]}mall_orders SET ordstate = 1 WHERE orderid='".$this->orderId."' AND offerid=".$this->shopId;
        $this->dbObj->Query($sqlString);
    }

    //订单部分送货
    function deliverany(){
        $sqlString = "UPDATE {$GLOBALS["_tbpre"]}mall_orders SET ordstate = 2 WHERE orderid='".$this->orderId."' AND offerid=".$this->shopId;
        $this->dbObj->Query($sqlString);
    }

    //订单已送货
    function delivered(){
        $sqlString = "UPDATE {$GLOBALS["_tbpre"]}mall_orders SET ordstate = 3 WHERE orderid='".$this->orderId."' AND offerid=".$this->shopId;
        $this->dbObj->Query($sqlString);
        //echo $sqlString;
    }

    //订单归档
    function logged()
    {
        $sqlString = "UPDATE {$GLOBALS["_tbpre"]}mall_orders SET ordstate = 4 WHERE orderid='".$this->orderId."' AND offerid=".$this->shopId;
        $this->dbObj->Query($sqlString);

        //删除订单中间库存状态
        $sqlString = "DELETE FROM {$GLOBALS["_tbpre"]}mall_goodsnotify
                    WHERE email='".$this->orderId."' AND offerid=".intval($this->shopId);
        $this->dbObj1->Query($sqlString);
    }

    //订单取消
    function cancel(){
        $sqlString = "UPDATE {$GLOBALS["_tbpre"]}mall_orders SET ordstate = 9 WHERE orderid='".$this->orderId."' AND offerid=".$this->shopId;
        $this->dbObj->Query($sqlString);

        //删除订单中间库存状态
        $sqlString = "DELETE FROM {$GLOBALS["_tbpre"]}mall_goodsnotify
                    WHERE email='".$this->orderId."' AND offerid=".intval($this->shopId);
        $this->dbObj->Query($sqlString);

        //退还积分
        if ($this->ordstate!=9)
        {
            $User = newclass("User");
            $User->userId = $this->userId;
            $User->getbyId();
            $User->point = $User->point + intval($this->consumepoint)+intval($this->chgpoint);
//            $User->point_history = $User->point_history + $this->consumepoint;
            $User->toUpdatelevel();
        }

    }

    //重新计算本订单额
    function calculate(){
        $sqlString = "select sum(oof) from {$GLOBALS["_tbpre"]}mall_items where orderid='".$this->orderId."' AND offerid=".$this->shopId;        //计算定单明细商品总价
        $this->dbObj->Query($sqlString);
        $this->dbObj->next_record();

        //更新定单价格信息
        $sqlString = "UPDATE {$GLOBALS["_tbpre"]}mall_orders SET item_amount= ".$this->dbObj->f(0);
        $sqlString.=", total_amount= freight+".$this->dbObj->f(0)." WHERE orderid='".$this->orderId."'";
        $this->dbObj->Query($sqlString);
    }

    //回复订单留言
    function updateReply(){
        $sqlString = "select reply from {$GLOBALS["_tbpre"]}mall_orders
                      where orderid = '".$this->orderId."' AND offerid = ".$this->shopId ;
        $this->dbObj->Query($sqlString);
        $this->dbObj->next_record();
        $tmp_reply = do_slash($this->dbObj->f(reply)) ;
        if(!empty($tmp_reply)){
            $tmp_reply .= "<br><br>" ;
        }

        $tmptime = date("Y-m-d H:i:s",mktime()) ;
        $sqlString = "UPDATE {$GLOBALS["_tbpre"]}mall_orders SET
                    reply= '".$tmp_reply.$this->reply."<br> -- ".$tmptime."', userrecsts = 2
                    WHERE orderid='".$this->orderId."' AND offerid=".$this->shopId;
        $this->dbObj->Query($sqlString);
    }

    //新增订单留言
    function updateMessage(){
        $sqlString = "select message from {$GLOBALS["_tbpre"]}mall_orders
                      where orderid = '".$this->orderId."' AND offerid = ".$this->shopId ;
        $this->dbObj->Query($sqlString);
        $this->dbObj->next_record();
        $tmp_message = do_slash($this->dbObj->f(message)) ;
        if(!empty($tmp_message)){
            $tmp_message .= "<br><br>" ;
        }

        $tmptime = date("Y-m-d H:i:s",mktime()) ;
        $sqlString = "UPDATE {$GLOBALS["_tbpre"]}mall_orders SET message= '".$tmp_message.$this->message."<br> -- ".$tmptime."',
                      userrecsts = 1, recsts = 5, feedbacktime = ".time()."
                      WHERE orderid='".$this->orderId."' AND offerid=".$this->shopId;
        $this->dbObj->Query($sqlString);
    }

    //用户更新订单取消
    function updateCancel(){
        $sqlString = "UPDATE {$GLOBALS["_tbpre"]}mall_orders SET
                    cancel= '".$this->canceltxt."', recsts = 2, userrecsts = 1, feedbacktime = ".time()."
                    WHERE orderid='".$this->orderId."' AND offerid=".$this->shopId;
        $this->dbObj->Query($sqlString);
    }

    //用户更新订单已收到货
    function updateOver(){
        $sqlString = "SELECT recsts FROM {$GLOBALS["_tbpre"]}mall_orders
                      WHERE orderid='".$this->orderId."' AND offerid=".$this->shopId;
        $this->dbObj->Query($sqlString);
        if ($this->dbObj->next_record())
        {
            $tmpRecsts = $this->dbObj->f("recsts");
            if ($tmpRecsts == 3) $tmpRecsts = 4;
            else $tmpRecsts = 1;

            $sqlString = "UPDATE {$GLOBALS["_tbpre"]}mall_orders SET recsts = $tmpRecsts, userrecsts = 1, feedbacktime = ".time()."
                      WHERE orderid='".$this->orderId."' AND offerid=".$this->shopId;
            $this->dbObj->Query($sqlString);
        }
    }

    //用户更新订单状态已付款
    function updatePayed(){
        $sqlString = "SELECT recsts FROM {$GLOBALS["_tbpre"]}mall_orders
                      WHERE orderid='".$this->orderId."' AND offerid=".$this->shopId;
        $this->dbObj->Query($sqlString);
        if ($this->dbObj->next_record())
        {
            $tmpRecsts = $this->dbObj->f("recsts");
            if ($tmpRecsts == 1) $tmpRecsts = 4;
            else $tmpRecsts = 3;
            $sqlString = "UPDATE {$GLOBALS["_tbpre"]}mall_orders SET recsts = $tmpRecsts, userrecsts = 1, feedbacktime = ".time()."
                      WHERE orderid='".$this->orderId."' AND offerid=".$this->shopId;
            $this->dbObj->Query($sqlString);
        }
    }

    //得出最新用户反馈数量
    function getUserrecsts(){
        $sqlString = "select * from {$GLOBALS["_tbpre"]}mall_orders
                      WHERE userrecsts = 1 AND offerid=".$this->shopId;
        $this->dbObj->Query($sqlString);
        return $this->dbObj->nf() ;
    }

    //更新用户反馈状态
    function updateUserrecsts()
    {
        $sqlString = "UPDATE {$GLOBALS["_tbpre"]}mall_orders SET userrecsts = 0
                      WHERE orderid='".$this->orderId."' AND offerid=".$this->shopId;
        $this->dbObj->Query($sqlString);
    }

    //输出购买成功的商品信息（在线支付后，自动更新订单状态）
    function getPayresult($ordid,$shopid,$deliver=false){
        global $PROG_TAGS;
        $Order = newclass("Order");
        $Order->shopId = $shopid;
        $Order->orderId = $ordid;
        if (!$Order->getbyId($ordid)){
            return "";
        }

        if($Order->payed == 1||$deliver||$Order->payed == 2){
            $class_oi = newclass("orderItems");
            $class_oi->orderId = $ordid;
            $class_oi->shopId = $shopid;
            $class_oi->getlist();                //读出订单各个商品

            $tmp_str = $PROG_TAGS["ptag_788"]." ".$class_oi->rowsFound." ".$PROG_TAGS["ptag_950"];
            $consign_id = "";
            while ($class_oi->next()){
                $this->dbObj->Query("SELECT * FROM {$GLOBALS["_tbpre"]}mall_goods WHERE gid='".$class_oi->gid."' AND offerid=".$shopid);
                if($this->dbObj->next_record()){
                    $tmp_object = $this->dbObj->f(ifobject);        //商品是否实体
                    $tmp_gid = $this->dbObj->f(gid);                //商品id
                    $tmp_name = $this->dbObj->f(goods);                //商品名称

                    if ($Order->ordstate < 2 && $consign_id == "" && ($tmp_object == 0 || $tmp_object == 2)){
                        //新建发货单
                        $consign = newclass("Consign");
                        $consign->offer_id = $shopid;             //商店ID
                        $consign->order_no = $ordid;              //订单号
                        $consign->send_id = $Order->delivery;       //配送方式ID
                        $consign->send_name = $Order->_delivery;    //配送方式名称
                        $consign->maker_id = 0;               //制单人ID
                        $consign->maker_name = "ONLINEPAY";   //制单人姓名
                        $consign->consign_date = time();      //发货时间
                        $consign->user_id = $Order->userId;           //下订单用户ID
                        $consign->user_name = $Order->name;           //下订单用户姓名
                        $consign->recv_name = $Order->name;           //收货人姓名
                        $consign->recv_add = $Order->address;         //收货地址
                        $consign->recv_zip = $Order->zip;             //收货邮编
                        $consign->recv_email = $Order->email;         //收货人EMAIL
                        $consign->recv_tel = $Order->tel;             //收货人电话
                        $consign->recv_mobile = $Order->mobile;       //收货人手机

                        $consign->logi_fee = $Order->freight;         //物流费用
                        $consign->return_reason = 0;    //退货原因
                        $consign->return_money = 0;        //是否退款
                        $consign->currency_code = $Order->paycur;    //币别
                        $consign->ex_rate = 1;                    //汇率
                        $consign->status = 1;                    //状态
                        $consign->memo = "ONLINE DELIVERY";        //备注
                        $consign_id = $consign->toInsert();
                    }

                    if ($tmp_object == 0)                    //虚拟商品
                    {
                        $mark_f = 1;        //设置订单中有非实体商品标记

                        $tmp_str .= "<br><br>".$PROG_TAGS["ptag_955"].$tmp_name.$PROG_TAGS["ptag_1459"];
                        $class_card = newclass("shopCards");
                        $class_card->shopId = $shopid;
                        $class_card->orderId = $ordid;
                        $tmp_num = 0 ;

                        //读取当前商品，已发货的数量
                        if ($class_card->getsaleGood($tmp_gid) > 0){
                            $sendmark = 1 ;        //当前商品已发货
                        }
                        else{
                            $sendmark = 0 ;        //当前商品未发货
                            $tmp_newnum = $class_card->getnewGood($tmp_gid);    //可发货数量
                        }

                        while ($class_card->next() && $class_oi->num > $tmp_num)
                        {
                            $tmp_str .= "<br><br>".$PROG_TAGS["ptag_959"]."".$class_card->decryptCard($class_card->cardNo);
                            $tmp_str .= "<br>".$PROG_TAGS["ptag_964"]."".$class_card->decryptCard($class_card->cardPsw);

                            if ($sendmark == 0)        //如果当前商品未发货，则继续发货
                            {
                                $class_card->orderId = $ordid;
                                $class_card->toSale();                //更改卡的销售状态
                                $tmp_num++;
                            }
                        }

                        if ($tmp_num < $class_oi->num && $sendmark == 0)
                        {
                            $tmp_str .= "<br><br>".$tmp_name.$PROG_TAGS["ptag_966"];
                            if ($tmp_num > 0){
                                $tmp_state = 2;
                                $class_oi->toSend(2);        //更新商品部分发货状态
                            }else{
                                if ($tmp_state > 0)
                                    $tmp_state = 2;
                                else
                                    $tmp_state = -1;
                            }
                        }else{
                            $class_oi->toSend(3);        //更新商品已发货状态
                            if ($tmp_state == 2 || $tmp_state == -1)
                                $tmp_state = 2;
                            else
                                $tmp_state = 3;
                        }

                        if ($consign_id > 0 && $tmp_num > 0){

                            $tmp_discreteness = "";
                            $orderDiscreteness = newclass("orderDiscreteness");
                            $orderDiscreteness->shopId = $shopid;
                            $orderDiscreteness->itemId = $class_oi->Id;
                            $orderDiscreteness->ordgId = $class_oi->productId;
                            $orderDiscreteness->getList();
                            while ($orderDiscreteness->next())
                            {
                                $tmp_discreteness .= "<br><i>".$orderDiscreteness->name." -- ".$orderDiscreteness->price * $orderDiscreteness->num." ( ".$orderDiscreteness->num." )</i>";
                            }

                            //新增发货明细
                            $consign_items = newclass("Consign_items");
                            $consign_items->offer_id = $shopid;   //商店ID
                            $consign_items->consign_id = $consign_id;   //对应主表ID
                            $consign_items->goodsid = $tmp_gid;   //商品ID
                            $consign_items->goodsname = $class_oi->name;   //商品名称
                            $consign_items->goodsdesc = $tmp_discreteness;   //商品说明
                            $consign_items->quantity = $tmp_num ;   //发货数量
                            $consign_items->price = $class_oi->price;   //单价(本币)
                            $consign_items->sumprice = $class_oi->price*$tmp_num;   //金额(本币)
                            $consign_items->toInsert();
                        }

                        if ($sendmark == 0)
                        {
                            $Product = newclass("Product");
                            $Product->toChgstorage($class_oi->Id, $tmp_gid, 1, $tmp_num);    //更改库存
                        }
                    }
                    elseif($tmp_object=2)
                    {
                        $mark_f = 1;
                        if($class_oi->digital_file!="")
                        {
                            $tmp_str .= "<br><br>".htmlspecialchars($class_oi->name)."&nbsp;<a href='index.php?gOo=download_digital.do&orderid={$ordid}&id=".$class_oi->Id."' target='_blank'>{$PROG_TAGS["download_digital"]}</a>";

                            if ($consign_id > 0){

                                $tmp_discreteness = "";
                                $orderDiscreteness = newclass("orderDiscreteness");
                                $orderDiscreteness->shopId = $shopid;
                                $orderDiscreteness->itemId = $class_oi->Id;
                                $orderDiscreteness->ordgId = $class_oi->productId;
                                $orderDiscreteness->getList();
                                while ($orderDiscreteness->next())
                                {
                                    $tmp_discreteness .= "<br><i>".$orderDiscreteness->name." -- ".$orderDiscreteness->price * $orderDiscreteness->num." ( ".$orderDiscreteness->num." )</i>";
                                }

                                //新增发货明细
                                $consign_items = newclass("Consign_items");
                                $consign_items->offer_id = $shopid;   //商店ID
                                $consign_items->consign_id = $consign_id;   //对应主表ID
                                $consign_items->goodsid = $tmp_gid;   //商品ID
                                $consign_items->goodsname = $class_oi->name;   //商品名称
                                $consign_items->goodsdesc = $tmp_discreteness;   //商品说明
                                $consign_items->quantity = 1 ;   //发货数量
                                $consign_items->price = $class_oi->price;   //单价(本币)
                                $consign_items->sumprice = $class_oi->price;   //金额(本币)
                                $consign_items->toInsert();
                            }
                            $tmp_state = 3;
                        }
                    }else
                        $mark_s = 1;        //设置订单中有实物商品标记
                }
                $class_oi->paystate = 1;
                $class_oi->toPay();        //更新商品已支付状态
            }
/********更改订单总状态*********/
            if ($mark_f == 1)    //有虚拟商品
            {
                if ($mark_s == 1 || $tmp_state == 2)    //订单中有实物商品 或者 有虚拟商品部分发货
                    $Order->deliverany();    //部分发货
                if ($tmp_state == 3)
                    $Order->delivered();    //已发货
            }
/********更改订单总状态*********/
        }else{
            $tmp_str .= "<br>NOT PAYED!";
        }

        return $tmp_str ;
    }

    //更新订单支付流水号
    function updatePayid($payid, $paytime)
    {
        $sqlString = "UPDATE {$GLOBALS["_tbpre"]}mall_orders SET payid = '".$payid."', paytime = '".$paytime
                    ."' WHERE orderid='".$this->orderId."' AND offerid=".$this->shopId;
        $this->dbObj->Query($sqlString);
    }

    //更新订单支付流水号
    function updateHistoryPayid($newpayid)
    {
        $sqlString = "UPDATE {$GLOBALS["_tbpre"]}mall_orders SET payid = '".$newpayid."',
                    historypayid = '".$this->historypayid.($this->historypayid != ""?",":"").$newpayid
                    ."' WHERE orderid='".$this->orderId."' AND offerid=".$this->shopId;
        $this->dbObj->query($sqlString);
    }

    //根据订单支付流水号得出商店订单号
    function getorderidbyPayid($payid)
    {
        $tmp_time = time();
        $sqlString = "select orderid, paymoney, paycur from {$GLOBALS["_tbpre"]}mall_orders WHERE payid = '".$payid."' AND paytime >= ".$tmp_time." - 86400 AND paytime < ".$tmp_time." + 86400";
        $this->dbObj->Query($sqlString);
        if ($this->dbObj->next_record())
        {
            $this->orderId = $this->dbObj->f("orderid");
            $this->paymoney = $this->dbObj->f("paymoney");
            $this->paycur = $this->dbObj->f("paycur");
            return $this->orderId;
        }
        else
            return false ;
    }
    //更新支付帐号
    function updatePayAccount($account)
    {
        $sqlString = "UPDATE {$GLOBALS["_tbpre"]}mall_orders SET payaccount = '".$account."'
                      WHERE orderid='".$this->orderId."' AND offerid=".$this->shopId;
        $this->dbObj->Query($sqlString);
    }

    function getordergetpoint($orderId) {//老版本兼容新版本4.72
        $sSql = "select sum(point) as point from {$GLOBALS["_tbpre"]}mall_point_history where offerid=".$this->shopId.' and related_id=\''.$orderId.'\' and type=3';
        $this->dbObj->Query($sSql);
        if(!$this->dbObj->next_record()){
            return 0;
        }else{
            return $this->dbObj->f("point");
        }
    }

    //签名订单
    function updateOrderSign($orderid, $sign)
    {
        if(!$orderid) return false;
        $sqlString = "UPDATE {$GLOBALS["_tbpre"]}mall_orders SET discount = ".$sign." WHERE orderid='".$orderid."'";
        $this->dbObj->Query($sqlString);
    }

    function getOrderBySign($sign){
		//error_log(print_r($sign,true),3,__FILE__.'.sign.log');	//alone
        $discount = intval($sign);
        if(gettype($sign) != 'array' && $discount != 0){
            $sqlString = "SELECT orderid FROM {$GLOBALS["_tbpre"]}mall_orders WHERE discount = ".$discount;
            $this->dbObj->Query($sqlString);
            if(!$this->dbObj->next_record()){
				//error_log(print_r('next_record',true),3,__FILE__.'.if.log');	//alone
                return false;
            }else{
                $this->orderId = $this->dbObj->f("orderid");
				//error_log(print_r('next_record',true),3,__FILE__.'.else.log');	//alone
                return true;
            }
        }else{
            return false;
        }
    }
    function getPayidByOrderno($orderno){
        $sqlString = "select payid FROM {$GLOBALS["_tbpre"]}mall_orders WHERE orderid='".$orderno."'";
        $this->dbObj->Query($sqlString);
        if (!$this->dbObj->next_record()){
            return false;
        }
        else{
            $this->payid = $this->dbObj->f("payid");
            return true;
        }
    }

	function transitPayed($orderid,$ifsk)
	{
		if(!$orderid) return false;
        $sqlString = "UPDATE {$GLOBALS["_tbpre"]}mall_orders SET ifsk = ".$ifsk." WHERE orderid='".$orderid."'";
        $this->dbObj->Query($sqlString);
	}
}
?>